# Models for OEST

# JAGS sampler for the four vignett descions in the austrian election study

# Required packages
library(dummies)

# Additional functions
source("Model_Data_oest.R") # transformes dataset in required jags format
source("Sampler_baysianmodel.R") # describes sampler

# Load Data-Set
load("oest_long.Rdata")

# Define quantaties
controls <- c("PID","gender","educ","relig","union","income","age" ) # Control Variables
N <- NULL

# For sampler
A <- 10000
U <- 8000
S <- 2000



#########################################################
# Models
#######################################################

 print("## \n Models \n ## \n\n")
 
 # OVP - Gruene
 #########
 
 cat("OVP - GRUENE \n ###### \n")
 
 # Create Data
 ogd <- Model.data(party = "ovp" ,coalition = "gruene"
                   ,vig = "wahl2"
                   ,rat.coal="rat.ovp.gruene"                  
                   ,covariates=controls
                   )

  N[1] <- nrow(ogd)
  
  # Model 1
  cat("Model 1 \n ###### \n")
  ovp.gruene <- Sample_Model(data=ogd, model.file = "Model_oest.bug"
                          , watch=c("gamma", "delta","beta", "diff","psi")
                          ,covariates=controls[-1]
                          , adapt = A
                          , update.iter = U
                          , sample.iter = S
                          )


  print(summary(ovp.gruene))



 # SPO - GRUENE
 ##########
 
 cat("SPO - GRUENE \n ###### \n")

 # Create Data
 sgd <- Model.data(party = "spo" ,coalition = "gruene"
                   ,vig = "wahl3"
                   ,rat.coal="rat.spo.gruene"
                   ,covariates=controls                   
                   )

  N[2] <- nrow(sgd)
  
   # Model 1
  cat("Model 1 \n ###### \n")
  spo.gruene <- Sample_Model(data=sgd, model.file = "Model_oest.bug"
                          , watch=c("gamma", "delta","beta", "diff","psi")
                          ,covariates=controls[-1]
                          , adapt = A
                          , update.iter = U
                          , sample.iter = S
                          )
  
  print(summary(spo.gruene))


 # OVP - FPO
 ##########
 
 cat("OVP - FPO \n ###### \n")

 # Create Data
 ofd  <- Model.data(party = "ovp" ,coalition = "fpo"
                   ,vig = "wahl4"
                   ,rat.coal="rat.ovp.fpo"
                   ,covariates=controls
                   )
 
 N[3] <- nrow(ofd)

  # Model 1
 cat("Model 1 \n ###### \n")
 ovp.fpo <- Sample_Model(data=ofd, model.file = "Model_oest.bug"
                        , watch=c("gamma", "delta","beta", "diff","psi")
                        ,covariates=controls[-1]
                        , adapt = A
                        , update.iter = U
                        , sample.iter = S
                        )
 
  print(summary(ovp.fpo))


 
 # SPO - FPO
 ##########

 # SPO - FPO has no coalition rating 
 
 cat("SPO - FPO \n ###### \n")

 # Create Data
 sfd  <- Model.data(party = "spo" ,coalition = "fpo"
                   ,vig = "wahl5"
                   ,rat.coal="rat.spo.fpo"
                   ,covariates=controls    
                   )

  # No Rating. Relies on model with opposite party rating instead (see discussion main text)
  names(sfd)[grep("rat",names(sfd))] <- c("Up1","Up2")
  
  N[4] <- nrow(sfd)


  # Model 1
  cat("Model 1 \n ###### \n")
  
  spo.fpo <- Sample_Model(data=sfd, model.file = "Model_oest2.bug"
                          , watch=c("gamma", "delta","beta", "diff","psi")
                          ,covariates=controls[-1]
                          , adapt = A
                          , update.iter = U
                          , sample.iter = S)
  
  print(summary(spo.fpo))
  


##########
#Save estimation
##########
  
 cat("\n Save results \n")
 save(
      ovp.gruene,spo.gruene,ovp.fpo,spo.fpo
      ,N
      ,file="models_oest_rat.Rdata")
 

# Process Time
  cat("\n It took me" , round(proc.time()[2]/60), "to run this script")
